package com.situ.web.servlet;

import java.io.IOException;
import java.io.PrintWriter;
import java.text.SimpleDateFormat;
import java.util.Date;

import javax.servlet.ServletException;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class LastAccessTimeServlet extends HttpServlet {
	@Override
	protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		Date date = new Date();
		SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
		String time = simpleDateFormat.format(date);
		Cookie cookie = new Cookie("time", time);
		resp.addCookie(cookie);	
		Cookie[] cookies = req.getCookies();
		String lastTime = null;
		if (cookies != null) {
			for (Cookie coo : cookies) {
				if ("time".equals(coo.getName())) {
					lastTime = coo.getValue();
				}
			}
		}
		
		resp.setContentType("text/html;charset=utf-8");
		PrintWriter writer = resp.getWriter();
		if (null == lastTime) {
			writer.println("第一次");
		} else {
			writer.println("上次登录时间" + lastTime);

		}
			
		}
		
		

}
